package ui.footer 
{
	import com.asual.swfaddress.SWFAddress;
	import com.gaiaframework.api.Gaia;
	import com.greensock.TweenMax;
	import com.somerandomdude.coordy.layouts.twodee.VerticalLine;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import model.Model;
	import pages.Pages;
	import utils.ShapeUtil;
	import utils.SpriteUtil;
	import utils.TextFieldUtil;
	
	/**
	 * ...
	 * @author usual
	 */
	public class FooterMenuItem extends MovieClip 
	{
		private var _data:XML;
		private var _ico:NavIcone;
		private var _txtField:TextField;
		private var _layout:VerticalLine;
		private var _containerSubMenu:MovieClip;
		public function FooterMenuItem(data:XML) 
		{
			_data = data;
			addEventListener(Event.ADDED_TO_STAGE, build);
			
		}
		
		private function build(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, build);
			addText();
			addPicto();
			if (_data.@id == 'communaute')
			{
				addSocialMenu();
				_txtField.mouseEnabled = false;
				addEventListener(MouseEvent.MOUSE_OVER, _onSocialOver);
				addEventListener(MouseEvent.MOUSE_OUT, _onSocialOut);
			}else
			{
				addEventListener(MouseEvent.MOUSE_OVER, _over);
				addEventListener(MouseEvent.MOUSE_OUT, _out);
				
				this.mouseChildren = false;
			}
			resize();
			this.cacheAsBitmap = true;
			this.buttonMode = true;
			this.addEventListener(MouseEvent.CLICK, _onClik);
		}
		
		private function _over(e:MouseEvent=null):void 
		{
			TweenMax.to(this, .5, { tint:0x01fbfb } );
		}
		
		private function _out(e:MouseEvent=null):void 
		{
			TweenMax.killTweensOf(this);
			TweenMax.to(this, .5, { removeTint:true } );
		}
		
		private function _onSocialOut(e:MouseEvent):void 
		{
			TweenMax.to(_containerSubMenu, .5, { autoAlpha:0 } );
		}
		
		private function _onSocialOver(e:MouseEvent):void 
		{
			trace("[FooterMenuItem._onSocialOver] ");
			TweenMax.to(_containerSubMenu, .5, { autoAlpha:1 } );
		}
		
		private function addSocialMenu():void 
		{
			//trace("[FooterMenuItem.addSocialMenu] ");
			var ln:int = Model.instance.getUiLabel("social").label.length();
			var sh:Shape = ShapeUtil.getLineH(10, 0xffffff);
			_layout = new VerticalLine(0);
			_containerSubMenu = new MovieClip();
			addChild(_containerSubMenu)
			for (var i:int = 0; i < ln; i++) 
			{
				var item:SubMenuItem = new SubMenuItem(Model.instance.getUiLabel("social").label[i]);
				
				_containerSubMenu.addChild(item);
				item.line.width = _txtField.width;
				_layout.addNode(item);
			}
			_containerSubMenu.y = -_containerSubMenu.height;
			_containerSubMenu.x = int(_ico.x + _ico.width + 10);
			TweenMax.to(_containerSubMenu, 0, { autoAlpha:0 } );
		}
		
		private function _onClik(e:MouseEvent):void 
		{
			if (_data.@id == 'home')
			{
				Gaia.api.getPage(Pages.NAV).content.footer.close();
				Gaia.api.goto(Pages.NAV + '/' + _data.@id);
			}
			if (_data.@id == 'photos')
			{
				Gaia.api.getPage(Pages.NAV).content.footer.close();
				Gaia.api.goto(Pages.PHOTOS);
			}
			if (_data.@id == 'videos')
			{
				Gaia.api.getPage(Pages.NAV).content.footer.close();
				Gaia.api.goto(Pages.VIDEOS);
			}
			if (_data.@id == 'contact')
			{
				var email:URLRequest = new URLRequest("mailto:"+String(Model.instance.getUiLabel('contactLink')));
				navigateToURL(email, "_blank");
			}
			Gaia.api.getPage(Pages.NAV).content._explanationMc.visible = false;
		}
		
		private function resize():void 
		{
			_txtField.x = int(_ico.x + _ico.width + 10);
		}
		
		private function addPicto():void 
		{
			_ico = new NavIcone();
			_ico.gotoAndStop(_data.@id);
			addChild(_ico);
		}
		
		private function addText():void 
		{
			_txtField = new TextField();
			_txtField.selectable = false;
			TextFieldUtil.setText(_txtField, "<nav>" + _data + "</nav>", true);
			addChild(_txtField);
		}
		
	}

}